define(["dojo/_base/declare", "esri/layers/TiledMapServiceLayer", "esri/geometry/Polygon", "esri/geometry/Extent","esri/geometry/Point", "esri/SpatialReference", "esri/layers/TileInfo","esri/tileUtils"],
    function (declare, TiledMapServiceLayer, Polygon, Extent, Point, SpatialReference, TileInfo,tileUtils) {
        return declare(TiledMapServiceLayer, {
            // 构造函数
            constructor: function (options) {
                this.spatialReference = options.spatialReference || new SpatialReference({
		           wkt: 'PROJCS["CGCS2000_3_Degree_GK_CM_117E",GEOGCS["GCS_China_Geodetic_Coordinate_System_2000",DATUM["D_China_2000",SPHEROID["CGCS2000",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Gauss_Kruger"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",118.5],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]'
		        });
                // 图层提供的起始显示范围以及整个图层的地理范围
                this.fullExtent = options.fullExtent;
                this.initialExtent = options.initialExtent;
				this.url = options.url;
				this.region = options.geometry;
				this.tileInfo = options.tileInfo;
                // 设置图层的loaded属性，并触发onLoad事件
                this.parseGeometry();
                this.loaded = true;
                this.onLoad(this);
            },
			parseGeometry(){
				this.levelsOfRegion = {}
				var lods = this.tileInfo.lods,n = lods.length,ring = this.region.rings[0];
            	for(var i=0;i<n;i++){
            		var m = ring.length,grids = [],gridIds = [];
            		lods[i].grids = []
            		this.levelsOfRegion[lods[i].level] = obj = {}
            		for(var j=0;j<m;j++){
            			var p = new Point(ring[j][0],ring[j][1],this.spatialReference);
            			var cell = tileUtils.getContainingTileCoords(this.tileInfo,p,lods[i]);
            			var gridId = cell.row + ',' +cell.col;
            			if(!gridIds.includes(gridId)){
            				gridIds.push(gridId);
            				grids.push([parseInt(cell.row),parseInt(cell.col)]);
            			}
            		}
//          		lods[i].grids = grids
					if(grids.length > 4){
						var polygonJson  = {"rings":[grids],"spatialReference":this.spatialReference};
						var polygon = new Polygon(polygonJson);
						obj.geometry = polygon;
						this.levelsOfRegion[lods[i].level] = obj
					}
            	}
			},
            getTileUrl: function (level, row, col) {
				var region = this.levelsOfRegion[level];
				var url = null;
				var point = new Point(row,col,this.spatialReference)
				//内接-contains
				if(region&&region.hasOwnProperty('geometry')&&region.geometry.contains(point)){
				//外接-intersect
				//if(region&&region.hasOwnProperty('geometry')&&tileUtils.getTileExtent(this.tileInfo, level, row, col).intersects(this.region)){
					//arcgis server sample like below
					//url = "http://183.250.128.116:5092/arcgis/rest/services/ysgw16/MapServer/tile/"+level+"/"+row+"/"+col;
                	url = this.url +"/tile/"+level+"/"+row+"/"+col;
                	return url;
				}
                //var url = "http://192.168.20.156:6080/arcgis/rest/services/donghai_img/MapServer/tile/"+level+"/"+row+"/"+col;
                //var url = "http://183.250.128.116:5092/arcgis/rest/services/ysgw16/MapServer/tile/"+level+"/"+row+"/"+col;
                return url
            }
        });
    }
)